//
//  HBNovelIntroCell.m
//  HeartBeat
//
//  Created by karong on 2023/8/28.
//

#import "HBNovelIntroCell.h"
@interface HBNovelIntroCell ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet UILabel *introLabel;
@property (nonatomic, strong) NSArray * dataArray;
@end

@implementation HBNovelIntroCell
- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setDataWithObject:(HBNovelModel *)object {
    self.introLabel.text = object.desc;
    NSArray * tArr = [object.tag componentsSeparatedByString:@","];
    NSMutableArray * tagArr = [NSMutableArray new];
    for (NSString * string in tArr) {
        if (!KIsNullOrEmpty(string)) {
            [tagArr addObject:string];
        }
    }
    self.dataArray = tagArr;
    [self.listView reloadData];
}
- (IBAction)descAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(novelIntroCellOpenFullDesc)]) {
        [self.delegate novelIntroCellOpenFullDesc];
    }
}

#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBBaseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBTagCell" forIndexPath:indexPath];
    UILabel * label = [cell viewWithTag:101];
    label.text = self.dataArray[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    NSString * title = self.dataArray[indexPath.row];
    CGFloat width = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12 weight:(UIFontWeightMedium)]} context:nil].size.width;
    size.width = width + 20;
    size.height = 26;
    return size;
}

@end
